package com.teamtgv.shareshark.task;

import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;

import com.teamtgv.shareshark.AppData;

import android.os.AsyncTask;
import android.util.Log;

public class ServerReachableTask extends AsyncTask<Void, Void, Boolean>{

	@Override
	protected Boolean doInBackground(Void... params) {
		boolean reachable = false;
		try{
		    SocketAddress sockaddr = new InetSocketAddress(AppData.HOST, 80);
		    Socket sock = new Socket();
            int timeoutMs = 6000;   // 6 seconds
		    sock.connect(sockaddr, timeoutMs);
		    Log.v("InternetChecker", "Connected to server");
		    reachable = true;
		} catch (Exception e) {
			Log.v("InternetChecker", "Error connecting to server: " + e.toString() + ": " + e.getMessage());
		}
		return reachable;
	}

}
